package com.retirement.staff.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.retirement.staff.domain.OldStaff;
import com.retirement.staff.dto.OldStaffDto;
import com.retirement.staff.mapper.OldStaffMapper;
import com.retirement.staff.service.IOldStaffService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lijy
 * @since 2023-12-28
 */
@Service
public class OldStaffServiceImpl extends ServiceImpl<OldStaffMapper, OldStaff> implements IOldStaffService {

    @Resource
    private OldStaffMapper oldStaffMapper;

    @Override
    public List<OldStaff> listOldStaff(OldStaffDto dto) {
        return oldStaffMapper.listOldStaff(dto);
    }

    @Override
    public int addOldStaff(OldStaff dto) {
        dto.setId(IdUtil.simpleUUID());
        dto.setCreateTime(LocalDateTime.now());
        dto.setUpdateTime(LocalDateTime.now());

        return baseMapper.insert(dto);
    }

    @Override
    public OldStaff getDetail(String id) {
        OldStaff oldStaff = oldStaffMapper.selectById(id);
        Assert.isTrue(ObjectUtil.isNotNull(oldStaff), "无此员工信息");

        return oldStaff;
    }

    @Override
    public int deleteStaff(String id) {
        OldStaff oldStaff = oldStaffMapper.selectById(id);
        Assert.isTrue(ObjectUtil.isNotNull(oldStaff), "该删除信息不存在，请联系管理员");
        oldStaff.setDeleted(1);

        return baseMapper.updateById(oldStaff);
    }

    @Override
    public int deleteUserByIds(String[] ids) {
        Assert.isTrue(ObjectUtil.isNotEmpty(ids),"参数不能为空！");
        Arrays.stream(ids).forEach(id->{
            OldStaff bean = baseMapper.selectById(id);
            bean.setDeleted(1);
            baseMapper.updateById(bean);
        });
        return 1;
    }

    @Override
    public int updateStaff(OldStaff dto) {
        OldStaff oldStaff = oldStaffMapper.selectById(dto.getId());
        Assert.isTrue(ObjectUtil.isNotNull(oldStaff), "无此员工信息");

        return baseMapper.updateById(dto);
    }


}
